﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using Common;
using Dto;

namespace SqlServerDAL
{
    public class UserAttentionDAL
    {
        private const string Waming = "SqlServerDAL.UserAttentionDAL";

        public bool Save(UserAttentionDto dto)
        {
            try
            {
                string sql;
                var parameters = new List<SqlParameter>
                                     {
                                         new SqlParameter("@UaUserId", SqlDbType.Int) {Value = dto.UaUserId},
                                         new SqlParameter("@UaConcernUserId", SqlDbType.Int)
                                             {Value = dto.UaConcernUserId},
                                         new SqlParameter("@UaTime", SqlDbType.DateTime) {Value = dto.UaTime}
                                     };
                if (dto.UaId != 0)
                {
                    sql =
                        "insert into UserAttention(UaUserId,UaConcernUserId,UaTime) values(@UaUserId,@UaConcernUserId,@UaTime)";
                }
                else
                {
                    sql =
                        "update UserAttention set UaUserId=@UaUserId,UaConcernUserId=@UaConcernUserId,UaTime=@UaTime where UaId=@UaId";
                    parameters.Add(new SqlParameter("@UaId", SqlDbType.Int) { Value = dto.UaId });
                }
                SqlHelper.ExecuteNonQuery(SqlHelper.connStr, CommandType.Text, sql, parameters.ToArray());
                return true;
            }
            catch (Exception ex)
            {
                Log.LogHandler.LogError(Waming + ".Save(UserAttentionDto dto)", ex);
                return false;
            }
        }

        public List<Hashtable> SlicedFindAll(int firstResult, int maxResult, int userId, int concernId, string order)
        {
            try
            {
                var hts = new List<Hashtable>();
                var whereSql = "";
                var parameters = new List<SqlParameter>();
                order = string.IsNullOrEmpty(order) ? "order by UaId desc" : order;
                var sql = "select row_number() over (" + order + ") as rowNumber,* from UserAttention ";
                if (userId != 0)
                {
                    whereSql += " where UaUserId=@UaUserId";
                    parameters.Add(new SqlParameter("@UaUserId", SqlDbType.Int) { Value = userId });
                }
                if (concernId != 0)
                {
                    whereSql += whereSql == ""
                                    ? " where UaConcernUserId=@UaConcernUserId"
                                    : " and UaConcernUserId=@UaConcernUserId";
                    parameters.Add(new SqlParameter("@UaConcernUserId", SqlDbType.Int) { Value = concernId });
                }
                sql = string.Format(
                    "select * from ({0} {1}) as UserAttention where rowNumber between {2} AND {3}",
                    sql, whereSql, firstResult, maxResult);
                using (var red = SqlHelper.ExecuteReader(SqlHelper.connStr, CommandType.Text, sql, parameters.ToArray())
                    )
                {
                    while (red.Read())
                    {
                        hts.Add(SqlHelper.FillHashtable(red));
                    }
                }
                return hts;
            }
            catch (Exception ex)
            {
                Log.LogHandler.LogError(Waming + ".SlicedFindAll(int firstResult, int maxResult, int userId, int concernId, string order)", ex);
                return null;
            }
        }

        public int SelectCount(int userId, int concernId)
        {
            try
            {
                const string sql = "select count(*) from UserAttention ";
                var whereSql = "";
                var parameters = new List<SqlParameter>();
                if (userId != 0)
                {
                    whereSql += " where UaUserId=@UaUserId";
                    parameters.Add(new SqlParameter("@UaUserId", SqlDbType.Int) { Value = userId });
                }
                if (concernId != 0)
                {
                    whereSql += whereSql == ""
                                    ? " where UaConcernUserId=@UaConcernUserId"
                                    : " and UaConcernUserId=@UaConcernUserId";
                    parameters.Add(new SqlParameter("@UaConcernUserId", SqlDbType.Int) { Value = concernId });
                }
                var red = SqlHelper.ExecuteScalar(SqlHelper.connStr, CommandType.Text, sql + whereSql,
                                                  parameters.ToArray());
                return red == DBNull.Value ? 0 : Convert.ToInt32(red);
            }
            catch (Exception ex)
            {
                Log.LogHandler.LogError(Waming + ".SelectCount(int userId, int concernId, string order)", ex);
                return -1;
            }
        }
    }
}
